package com.test.deencredit.dialog_deencredit;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.test.deencredit.R;
import com.test.deencredit.adapter_deencredit.AccountChannelViewAdapter_deencredit;
import com.test.deencredit.bean_deencredit.config_deencredit.AccountChannel_Fr_deencredit;
import com.test.deencredit.bean_deencredit.config_deencredit.AppShowCustomize_deencredit;
import com.test.deencredit.config_deencredit.CountryConfig_deencredit;
import com.test.deencredit.listener_deencredit.BindAccountListener_Fr_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

public class BindAccountDialog_Fr_deencredit extends Dialog {

    private FragmentActivity activity_deencredit;

    private final String page_deencredit;

    private final BindAccountListener_Fr_deencredit bindAccountListener_deencredit;

    private final AccountChannelViewAdapter_deencredit accountChannelViewAdapter_deencredit;

    public BindAccountDialog_Fr_deencredit(@NonNull FragmentActivity activity, String page, BindAccountListener_Fr_deencredit bindAccountListener) {
        super(activity);
        this.activity_deencredit = activity;
        this.page_deencredit = page;
        this.bindAccountListener_deencredit = bindAccountListener;

        accountChannelViewAdapter_deencredit = new AccountChannelViewAdapter_deencredit(AppShowCustomize_deencredit.getInstance_deencredit().getAccountChannels_deencredit(),page);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bindaccount_fr_deencredit);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView_deencredit();
    }

    @SuppressLint("SetTextI18n")
    private void initView_deencredit(){

        findViewById(R.id.dbf_ib_close).setOnClickListener(v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"DIALOG_CLOSE_CLICK","");
            dismiss();
        });

        //初始化渠道选择器
        RecyclerView channelRv = findViewById(R.id.dbf_recyclerview);
        channelRv.setAdapter(accountChannelViewAdapter_deencredit);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);//网格布局管理器
        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        channelRv.setLayoutManager(gridLayoutManager);

        //绑卡输入
        TextView areacode1Tv = findViewById(R.id.dbf_tv_areacode1);
        TextView areacode2Tv = findViewById(R.id.dbf_tv_areacode2);
        EditText mobile1Et = findViewById(R.id.dbf_et_mobile1);
        EditText mobile2Et = findViewById(R.id.dbf_et_mobile2);

        mobile1Et.setOnFocusChangeListener((v, hasFocus) ->
                HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"DIALOG_PHONE_INPUT",hasFocus ? "":mobile1Et.getText().toString().trim())
        );

        mobile2Et.setOnFocusChangeListener((v, hasFocus) ->
                HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"DIALOG_PHONE_CONFIRM_INPUT",hasFocus ? "":mobile2Et.getText().toString().trim())
        );

        areacode1Tv.setText(CountryConfig_deencredit.getInstance_deencredit().getAreaCode_deencredit());
        areacode2Tv.setText(CountryConfig_deencredit.getInstance_deencredit().getAreaCode_deencredit());

        //绑卡提交
        findViewById(R.id.dbf_btn_add).setOnClickListener(v -> {

            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"DIALOG_SAVE_CLICK","");

            String mobile1 = mobile1Et.getText().toString().trim();
            String mobile2 = mobile2Et.getText().toString().trim();

            if (TextUtils.isEmpty(mobile1)) mobile1 = "";
            if (TextUtils.isEmpty(mobile2)) mobile2 = "";

            AccountChannel_Fr_deencredit selectChannel = accountChannelViewAdapter_deencredit.getSelectChannel_deencredit();
            String channel = selectChannel==null ? "":selectChannel.getValue_deencredit();

            String finalMobile = mobile1;

            HttpUtil_deencredit.getInstance_deencredit().addAccount_Fr_deencredit(
                    activity_deencredit,
                    mobile1,
                    mobile2,
                    channel,
                    () -> {if (bindAccountListener_deencredit !=null) bindAccountListener_deencredit.onBindAccount_deencredit(finalMobile);},
                    false
            );

            dismiss();

        });

    }

}
